package com.liwenfeng.tcpandupd;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CompletableFuture;

public class BSServer {
    public static void main(String[] args) throws IOException {
        new BSServer().server();
    }

    public void server() throws IOException {
        System.out.println("服务端启动了...");
        try (
                ServerSocket serverSocket = new ServerSocket(8888);
        ) {

            while (true) {
                Socket acceptSocket = serverSocket.accept();
                System.out.println("有人上线了..." + acceptSocket.getRemoteSocketAddress());
                CompletableFuture.runAsync(() -> {
                    OutputStream os = null;
                    try {
                        os = acceptSocket.getOutputStream();
                        PrintStream ps = new PrintStream(os);
                        ps.println("HTTP/1.1 200 OK");
                        ps.println("Content-Type:text/html;charset=UTF-8");
                        ps.println();
                        ps.println("<H2>服务端响应的数据</H2>");
                        acceptSocket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        }
    }
}
